#!/bin/sh

mkdir -p %OPTWARE_TARGET_PREFIX%/var/stunnel

chmod 777 %OPTWARE_TARGET_PREFIX%/var/stunnel

if [ ! -f %OPTWARE_TARGET_PREFIX%/etc/stunnel/stunnel.pem ] ;  then

    echo
    echo "Creating %OPTWARE_TARGET_PREFIX%/etc/stunnel/stunnel.pem (server certificate) ..."

    mkdir -p %OPTWARE_TARGET_PREFIX%/etc/stunnel

    %OPTWARE_TARGET_PREFIX%/bin/openssl req -new -x509 -days 365 -nodes -rand /dev/urandom \
	-config %OPTWARE_TARGET_PREFIX%/etc/stunnel/stunnel-cert.cnf \
	-out %OPTWARE_TARGET_PREFIX%/etc/stunnel/stunnel.pem -keyout %OPTWARE_TARGET_PREFIX%/etc/stunnel/stunnel.pem

    %OPTWARE_TARGET_PREFIX%/bin/openssl x509 -subject -dates -fingerprint -noout -in %OPTWARE_TARGET_PREFIX%/etc/stunnel/stunnel.pem

fi

%OPTWARE_TARGET_PREFIX%/etc/init.d/S68stunnel
